#include "weatherwindow.h"
#include "ui_weatherwindow.h"
#include "common.h"

#include <QDateTime>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

WeatherWindow::WeatherWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WeatherWindow)
{
    ui->setupUi(this);

    //调用函数
    showTime();  //进入天气页面时展示时间
    initVector();  //进入天气页面时初始化容器
    getWeather();  //进入天气页面时获取天气
}

WeatherWindow::~WeatherWindow()
{
    delete ui;
}

//返回主页面按钮槽函数
void WeatherWindow::on_button_back_clicked()
{
    this->hide();
    this->parentWidget()->show();
}

//更新时间槽函数
void WeatherWindow::updateTime()
{
    QDateTime dateTime=QDateTime::currentDateTime();
    QString time=dateTime.toString("HH:mm:ss");

    ui->label_time->setText(time);
    if((time.split(":")[0].toInt())>6 && (time.split(":")[0].toInt())<18)
    {
        ui->label_timeName->setText("白天");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/day.png);");
    }
    else if((time.split(":")[0].toInt())>18 && (time.split(":")[0].toInt())<23)
    {
        ui->label_timeName->setText("夜间");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/night.png);");
    }
    else
    {
        ui->label_timeName->setText("凌晨");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/night.png);");
    }
}

//更新天气槽函数
void WeatherWindow::updateWeather(QNetworkReply *reply)
{
    QJsonDocument document=QJsonDocument::fromJson(reply->readAll());
    QJsonObject obj=document.object();

    //通过json对象获取属性值
    QString getCity=obj.value("city").toString();
    ui->label_city->setText(getCity);  //城市

    QJsonArray dataArr=obj.value("data").toArray();
    for(int i=0;i<dataArr.count();i++)
    {
        QJsonObject dateObj=dataArr.at(i).toObject();

        QString getDay=dateObj.value("day").toString();
        this->dates->at(i)->setText(getDay.split("（")[0]);  //日期
        this->weeks->at(i)->setText(getDay.split("（")[1].split("）")[0]);  //星期

        QString minTemperature=dateObj.value("tem2").toString();
        QString maxTemperature=dateObj.value("tem1").toString();
        QString getTemperature=minTemperature.append("℃").append("～").append(maxTemperature).append("℃");
        this->temperatures->at(i)->setText(getTemperature);  //一天温度范围

        QString getWeather=dateObj.value("wea").toString();
        this->weathers->at(i)->setText(getWeather);  //天气

        QString getIcon=dateObj.value("wea_img").toString();
        QString styleSheet=QString(R"(border-image: url(:/resources/weather/%1.png);)").arg(getIcon);
        this->icons->at(i)->setStyleSheet(styleSheet);  //天气图标

        if(i==0)  //今天的天气
        {
            ui->label_today_icon->setStyleSheet(styleSheet);  //天气图标

            this->weeks->at(i)->setText(getDay.split("（")[1].split("）")[0].append("（今天）"));  //星期X（今天）

            QString getTodayTemperature=dateObj.value("tem").toString();
            ui->label_today_temperature->setText(getTodayTemperature.append("℃"));  //今日平均气温

            ui->label_today_weatherName->setText(getWeather);
        }
    }
}

//展示时间函数
void WeatherWindow::showTime()
{
    QDateTime dateTime=QDateTime::currentDateTime();
    QString time=dateTime.toString("HH:mm:ss");

    ui->label_time->setText(time);
    if((time.split(":")[0].toInt())>6 && (time.split(":")[0].toInt())<18)
    {
        ui->label_timeName->setText("白天");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/day.png);");
    }
    else if((time.split(":")[0].toInt())>18 && (time.split(":")[0].toInt())<23)
    {
        ui->label_timeName->setText("夜间");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/night.png);");
    }
    else
    {
        ui->label_timeName->setText("凌晨");
        ui->label_timeIcon->setStyleSheet("border-image: url(:/resources/weather/night.png);");
    }

    //定时器，每隔1s获取一次时间
    QTimer *timer=new QTimer();
    connect(timer,&QTimer::timeout,this,&WeatherWindow::updateTime);
    timer->start(1000);  //1000ms

}

//初始化容器函数
void WeatherWindow::initVector()
{
    //初始化日期容器
    this->dates=new vector<QLabel*>();
    dates->emplace_back(ui->label_day1_date);
    dates->emplace_back(ui->label_day2_date);
    dates->emplace_back(ui->label_day3_date);
    dates->emplace_back(ui->label_day4_date);
    dates->emplace_back(ui->label_day5_date);
    dates->emplace_back(ui->label_day6_date);
    dates->emplace_back(ui->label_day7_date);

    //初始化周容器
    this->weeks=new vector<QLabel*>();
    weeks->emplace_back(ui->label_day1_week);
    weeks->emplace_back(ui->label_day2_week);
    weeks->emplace_back(ui->label_day3_week);
    weeks->emplace_back(ui->label_day4_week);
    weeks->emplace_back(ui->label_day5_week);
    weeks->emplace_back(ui->label_day6_week);
    weeks->emplace_back(ui->label_day7_week);

    //初始化天气图标容器
    this->icons=new vector<QLabel*>();
    icons->emplace_back(ui->label_day1_icon);
    icons->emplace_back(ui->label_day2_icon);
    icons->emplace_back(ui->label_day3_icon);
    icons->emplace_back(ui->label_day4_icon);
    icons->emplace_back(ui->label_day5_icon);
    icons->emplace_back(ui->label_day6_icon);
    icons->emplace_back(ui->label_day7_icon);

    //初始化天气容器
    this->weathers=new vector<QLabel*>();
    weathers->emplace_back(ui->label_day1_weatherName);
    weathers->emplace_back(ui->label_day2_weatherName);
    weathers->emplace_back(ui->label_day3_weatherName);
    weathers->emplace_back(ui->label_day4_weatherName);
    weathers->emplace_back(ui->label_day5_weatherName);
    weathers->emplace_back(ui->label_day6_weatherName);
    weathers->emplace_back(ui->label_day7_weatherName);

    //初始化温度容器
    this->temperatures=new vector<QLabel*>();
    temperatures->emplace_back(ui->label_day1_temperature);
    temperatures->emplace_back(ui->label_day2_temperature);
    temperatures->emplace_back(ui->label_day3_temperature);
    temperatures->emplace_back(ui->label_day4_temperature);
    temperatures->emplace_back(ui->label_day5_temperature);
    temperatures->emplace_back(ui->label_day6_temperature);
    temperatures->emplace_back(ui->label_day7_temperature);
}

//获取天气数据函数
void WeatherWindow::getWeather()
{
    QNetworkAccessManager *manager=new QNetworkAccessManager();

    //向天气api发起get请求
    QString url=QString(WEATHER_URL).arg(APPID).arg(APPSECRET);
    manager->get(QNetworkRequest(QUrl(url)));

    //manager发送结束信号，updateWeather接收
    connect(manager,&QNetworkAccessManager::finished,this,&WeatherWindow::updateWeather);
}

//更新天气按钮槽函数
void WeatherWindow::on_button_locate_clicked()
{
    getWeather();
}

